#include "header.h"

class Rational
{
	int x;
	int y; 
public:
	Rational(int=0, int=1); 
	Rational(const Rational&);
	~Rational(); 
	void afisare();
	operator float();
	friend Rational operator +(Rational, Rational);
	friend Rational operator *(Rational, Rational);
	friend Rational operator -(Rational, Rational);
	friend Rational operator /(Rational, Rational);
	friend void Adunare(Rational &,Rational &);
	friend void Scadere(Rational &,Rational &);
	friend void Inmultire(Rational &,Rational &);
	friend void Impartire(Rational &,Rational &);
	friend int diviz(int,int);
};

Rational::Rational(int x, int y)
{
	this->x = x;
	this->y = y;
}

Rational::~Rational()
{
}

Rational::Rational(const Rational& z)
{
	this->x = z.x;
	this->y = z.y;
}
void Rational::afisare()
{
	cout << x << "/" << y << " ";
}

Rational::operator float()
{
	return x/(float)y;
}

Rational operator +(Rational c1, Rational c2)
{
	Rational tmp;
	tmp.x=c1.x*c2.y+c2.x*c1.y;
	tmp.y=c1.y*c2.y;
	return tmp;
}

Rational operator /(Rational c1, Rational c2)
{
	Rational tmp;
	tmp.x=c1.x*c2.y;
	tmp.y=c1.y*c2.x;
	return tmp;
}

Rational operator -(Rational c1, Rational c2)
{
	Rational tmp;
	tmp.x=c1.x*c2.y-c2.x*c1.y;
	tmp.y=c1.y*c2.y;
	return tmp;
}

Rational operator *(Rational c1, Rational c2)
{
	Rational tmp;
	tmp.x=c1.x*c2.x;
	tmp.y=c1.y*c2.y;
	return tmp;
}

void Adunare(Rational &nr1,Rational &nr2)
{
	Rational tmp;
	int aux;
	tmp=nr1+nr2;
	aux=diviz(abs(tmp.x),abs(tmp.y));
	while(aux!=1)
	{
		tmp.x/=aux;
		tmp.y/=aux;
		aux=diviz(tmp.x,tmp.y);
	}
	nr1.afisare();
	cout<<"+";
	nr2.afisare();
	cout<<"=";
	tmp.afisare();
	cout<<endl;
}

void Scadere(Rational &nr1,Rational &nr2)
{
	Rational tmp;
	int aux;
	tmp=nr1-nr2;
	aux=diviz(abs(tmp.x),abs(tmp.y));
	while(aux!=1)
	{
		tmp.x/=aux;
		tmp.y/=aux;
		aux=diviz(tmp.x,tmp.y);
	}
	nr1.afisare();
	cout<<"-";
	nr2.afisare();
	cout<<"=";
	tmp.afisare();
	cout<<endl;
}

void Inmultire(Rational &nr1,Rational &nr2)
{
	Rational tmp;
	int aux;
	tmp=nr1*nr2;
	aux=diviz(abs(tmp.x),abs(tmp.y));
	while(aux!=1)
	{
		tmp.x/=aux;
		tmp.y/=aux;
		aux=diviz(tmp.x,tmp.y);
	}
	nr1.afisare();
	cout<<"*";
	nr2.afisare();
	cout<<"=";
	tmp.afisare();
	cout<<endl;
}

void Impartire(Rational &nr1,Rational &nr2)
{
	Rational tmp;
	int aux;
	tmp=nr1/nr2;
	aux=diviz(abs(tmp.x),abs(tmp.y));
	while(aux!=1)
	{
		tmp.x/=aux;
		tmp.y/=aux;
		aux=diviz(tmp.x,tmp.y);
	}
	nr1.afisare();
	cout<<"/";
	nr2.afisare();
	cout<<"=";
	tmp.afisare();
	cout<<endl;
}


int diviz(int nr1,int nr2)
{
	while(nr1!=nr2)
	{
		if(nr1>nr2)
			nr1-=nr2;
		else
			nr2-=nr1;
	}
	return nr1;
}

int main()
{
	Rational r1(1,2), r2(3,4),r3(2,5),r4(3,4),r5(3,4),r6(16,15),r7(2,5),r8(7,4);
	Adunare(r1,r2);
	Scadere(r3,r4);
	Inmultire(r5,r6);
	Impartire(r7,r8);
	_getch();
	return 0;
}